/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.button.renderer;

import com.jb.web.bean.Image;
import com.jb.web.context.WebContext;
import com.jb.web.renderer.DefaultRenderer;

import com.jb.faces.component.UIComponent;


public class ToolbuttonRenderer extends DefaultRenderer {
    public StringBuffer render(WebContext pCtx, UIComponent item) {
        Image hItem = (Image) item;
        StringBuffer htmlBuffer = new StringBuffer("");

        if (!isVisible(pCtx, hItem)) {
            return htmlBuffer;
        }

        String src = hItem.getLocation();

        if (hItem.isDisabled() && (hItem.getDisableImage() != null)) {
            src = hItem.getDisableImage();
        }

        src = evaluate(pCtx, hItem, src);

        if (isEmptyString(src) && !isEmptyString(hItem.getDefaultValue())) {
            src = "" + evaluate(pCtx, hItem, hItem.getDefaultValue());
        }

        String url = hItem.getDestination();
        boolean isLink = !isEmptyString(url) && (pCtx.isRunTime() || !hItem.isDesignable()) && !hItem.isDisabled();

        if (isEmptyString(src)) {
            src = "resources/images/noimages.jpg";
        }

        if (isLink) {
            url = (String) evaluate(pCtx, hItem, url);
            url = pCtx.formURL(url, hItem);
            htmlBuffer.append("<a ").append(addTagProperty("href", url, null))
                      .append(addTagProperty("class", "imglink", null))
                      .append(addTagProperty("id", hItem.getClientId() + "_ref", null)).append(">");
        }

        String desc = (String) evaluate(pCtx, hItem, hItem.getTitle());

        if (!isEmptyString(src) || !pCtx.isRunTime()) {
            hItem.setBorderStyle("border:solid 1px white;");
            htmlBuffer.append("<img ").append(addTagProperty("src", src, null))
                      .append("onmouseover=\"this.style.border='outset 1px white';\" onmousedown=\"this.style.border='inset 1px white';\" onmouseup=\"this.style.border='solid 1px white';\" onmouseout=\"this.style.border='solid 1px white';\"")
                      .append(addTagProperty("style", hItem.getStyle(pCtx), null))
                      .append(addTagProperty("class", hItem.getStyleClass(), null))
                      .append(addTagProperty("title", desc, null)).append(getEditScript(pCtx, hItem)).append(" />");
        }

        if (isLink) {
            htmlBuffer.append("</a>");
        }

        return htmlBuffer;
    }
}
